package com.chinaums.xm.wechat.service.wechat;

import java.util.Map;
import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.chinaums.xm.wechat.WeixinConstant;
import com.chinaums.xm.wechat.commons.FileUtil;
import com.chinaums.xm.wechat.domain.wechat.AccessToken;

import net.sf.json.JSONObject;

/**
 * 素材管理service
 * @author chenjf
 *
 */
@Service
public class MaterialService {
	
	@Resource
	private CommonService commonService;
	/**
	 * 临时素材上传
	 * @param mediaType
	 * @param localFilePath
	 * @param serverFieldName
	 * @param params
	 * @return
	 */
	public Optional<String> mediaUpLoad(final String mediaType,final String localFilePath,
			final String serverFieldName,final Map<String,String> params){
		final Optional<AccessToken> accessToken = commonService.getAccessToken();
		if(accessToken.isPresent()){
			final String token = accessToken.get().getToken();
			final String uploadUrl = WeixinConstant.MEDIA_UPLOAD_URL.replace("ACCESS_TOKEN", token).replace("TYPE", mediaType);
			try {
				final String responseMsg = FileUtil.uploadFile(uploadUrl, localFilePath, serverFieldName, params);
				JSONObject jSONObject = JSONObject.fromObject(responseMsg);
				final String mediaId = jSONObject.getString("media_id");
				return Optional.of(mediaId);
			} catch (Exception e) {
				return Optional.empty();
			}
		} else {
			return Optional.empty();
		}
	}
	
	/**
	 * 群发-上传图片获取图片URL接口，URL用于群发图文消息
	 */
	public void uploadImage(){
	}
}
